home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9463 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  81 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: malloc question
  5. Date: 10 Mar 1996 12:16:24 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hvdaoINN1im@keats.ugrad.cs.ubc.ca>
  8. References: <4htonk$350@news.hklink.net> <4huctt$arv@sparcserver.lrz-muenchen.de> <314318AF.30F@iperbole.bologna.it>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <314318AF.30F@iperbole.bologna.it>,
  12. Enrico Persiani  <vos0225@iperbole.bologna.it> wrote:
  13.  
  14. [ reformatted to 79 columns ]
  15.  
  16. >> Simple answer: You don't have to use that cast, and you should _not_ use
  17. >> it, because all you can do with that cast is _hide_ an error.
  18.  
  19. >Error?!? Why do you think that casting is an error? The standard ANSI runtime
  20. >library says:
  21.  
  22. [ snip ]
  23.  
  24. >So, if he needs to use that pointer returned by malloc function he have to
  25. >convert it from a 'void' pointer to a 'item' pointer ( PITEM ).  Many compilers
  26. >don't need the use of casting. It's only a good programming style. But
  27.  
  28. _Standard_ compilers don't need the cast. The ANSI standard blesses void * as
  29. an object which can store a pointer of any type.
  30.  
  31. >remember: malloc returns a 'void' pointer because it doesn't know wich kind of
  32. >data you'll store in the allocated mem block!
  33.  
  34. Correction: it returns a _strictly aligned_ void pointer. Just any old void
  35. pointer cannot be converted to point to arbitrary data types.  I could cast a
  36. char pointer to void, and then cause a serious error by converting it to a
  37. pointer to int, for example.
  38.  
  39. >> OTOH, if you are using a C++ compiler to translate C programs, the cast
  40. >> is needed, but malloc() is obsolete.
  41.  
  42. >C++ compilers are more rigorous! But if you write a strict-ANSI C program a c++
  43. >compiler won't return any error...
  44.  
  45. Even if your C program uses as variable names those identifiers which represent
  46. reserved identifiers in C++?
  47.  
  48. What does your C++ compiler do when I feed it this following ANSI/ISO compliant
  49. program?
  50.  
  51. #include <stdio.h>
  52.  
  53. typedef int new;
  54.  
  55. int main()
  56. {
  57.     new x = 3;
  58.     return x;
  59. }
  60.  
  61. An ANSI compiler will compile the above. What about GNU g++?
  62.  
  63. test4.c:3: parse error before `new'
  64. test4.c: In function `int main()':
  65. test4.c:7: parse error before `='
  66. test4.c:8: `x' undeclared (first use this function)
  67. test4.c:8: (Each undeclared identifier is reported only once
  68. test4.c:8: for each function it appears in.)
  69.  
  70. How about the HP-UX CC compiler (which uses cfront to translate C++ to C?)
  71.  
  72.  
  73. CC: "test4.c", line 3: error: syntax error (1502)
  74. CC: "test4.c", line 7: error: syntax error (1502)
  75. CC: sorry, cannot recover from previous errors
  76.  
  77.  
  78. Do you still believe that C++ compilers accept ANSI C programs? 
  79. -- 
  80.  
  81.